import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.*;

public class db_query_handler {
	private Socket oSock;
	private long iQueryStart = 0;
	private int iState = 0;
	private String sSQL;
	private BufferedOutputStream oConOut;
	private BufferedReader oConIn;
	db_query_handler (Socket oSocket) {
		oSock = oSocket;
	}
	public String get_SQL() {
		return sSQL;
	}
	public boolean get_next_query() {
		
		if (oSock.isClosed() || iState == 0)
			return false;

		String[] sHeaderParts = null;
		int iDataLength = 0;
		char[] bData = null;

		try {
			String sRequestHeader = oConIn.readLine();

			iQueryStart = System.currentTimeMillis();
			if (sRequestHeader != null && sRequestHeader.equals("QUERY QDBP/1.0")) {

				while (!(sRequestHeader = oConIn.readLine()).equals("")){
					
					sHeaderParts = sRequestHeader.split(": ");
					if (sHeaderParts.length != 2) {
						System.out.println("Fail1");
						oConOut.write("402 ERR QDBP/1.0\ndescription: Malformed header, read the QDBP documentation for available commands.\n\n".getBytes());
						oConOut.flush();
						oSock.close();
					} else {
						if (sHeaderParts[0].equals("length")){
							try {
								iDataLength = Integer.parseInt(sHeaderParts[1]);

							} catch (NumberFormatException e) {
								System.out.println("Fail2");
								oConOut.write("403 ERR QDBP/1.0\ndescription: Malformed header data, read the QDBP documentation for available commands.\n\n".getBytes());
								oConOut.flush();
								oSock.close();
							}
						}
					}
				}
				if (iDataLength > 0) {
					bData = new char[iDataLength];
					oConIn.read(bData);
					sSQL = new String(bData);
					return true;
				}

			}
		}
		catch (Exception e) {e.printStackTrace();}
		return false;
	}
	public void add_response(byte[] bData) {
		try {
			oConOut.write(bData);
			oConOut.flush();
			/*
			byte[] oBytes = new byte[db_util.DB_PAGE_SIZE];
			for (int i = 0; i < bData.length; i += db_util.DB_PAGE_SIZE) {
				System.out.println("printing a packet");
				int iTo =  db_util.DB_PAGE_SIZE;
				if (iTo + i *  db_util.DB_PAGE_SIZE > bData.length)
					iTo = bData.length - i *  db_util.DB_PAGE_SIZE;
				System.arraycopy(bData, 0 * db_util.DB_PAGE_SIZE, oBytes, 0, iTo);
				oConOut.write(oBytes);
				oConOut.flush();
			}
			*/
		} catch (Exception e) {
			e.printStackTrace();
		}


	}
	public boolean helo(){
		String sRequestHeader = "";
	
		try {

			oConOut = new BufferedOutputStream(oSock.getOutputStream());
			oConIn = new BufferedReader(new InputStreamReader(oSock.getInputStream()));
			sRequestHeader = oConIn.readLine();

			if (iState == 0 & sRequestHeader.equals("HELO QDBP/1.0")) {
				if ((sRequestHeader = oConIn.readLine()).equals("")) { 

					oConOut.write("200 OK QDBP/1.0\n\n".getBytes());
					oConOut.flush();
					iState = 1;
					return true;
				} else {
					oConOut.write("401 ERR QDBP/1.0\ndescription: Unknown command, read the QDBP documentation for available commands.\n\n".getBytes());
					oConOut.flush();
					oSock.close();
					return false;
				}
			} else {
				oConOut.write("401 ERR QDBP/1.0\ndescription: Unknown command, read the QDBP documentation for available commands.\n\n".getBytes());
				oConOut.flush();
				oSock.close();
				return false;
			}
		}
		catch (Exception e) {
			return false;
		}
	}

}